<template>
  <div class="popup" v-if="popupVisible">
    <transition
      leave-active-class="animated fadeOut"
      enter-active-class="animated fadeIn"
    >
      <div class="popup-bg" @click.stop.prevent="hide" v-show="popupVisible"></div>
    </transition>
    <transition
      leave-active-class="animated slideOutDown"
      enter-active-class="animated slideInUp">
      <div class="popup-wrapper" v-show="visible">
        <div class="popup-title" v-if="title && title.length > 0">{{title}}</div>
        <div class="popup-btn"
             :class="{'danger': item.type === 'danger'}"
             v-for="(item, index) of btn"
             :key="index"
             @click="item.click"
        >{{item.text}}</div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Popup',
  props: {
    title: String,
    btn: Array
  },
  data () {
    return {
      popupVisible: false,
      visible: false
    }
  },
  methods: {
    show () {
      this.popupVisible = true
      // 由于使用V-if,Dom是需要渲染的，当dom还没渲染成功，是无法进行动画，可以延迟
      setTimeout(() => {
        this.visible = true
      })
    },
    hide () {
      this.visible = false
      //  由于popup会直接消除，所以无法看到动画，可延迟消除
      setTimeout(() => {
        this.popupVisible = false
      }, 200)
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '../../assets/styles/global.styl'
  .popup
    position: fixed
    top: 0
    left: 0
    right: 0
    bottom: 0
    z-index: 2000
    width: 100%
    height: 100%
    background: rgba(0, 0, 0, .4)
    .popup-bg
      width: 100%
      height: 100%
    .popup-wrapper
      position: fixed
      left: 0
      bottom: 0
      z-index: 2000
      width: 100%
      background: white
      .popup-title
        width: 100%
        height: px2rem(44)
        border-bottom: px2rem(1) solid #eee
        font-size: px2rem(12)
        line-height: px2rem(14)
        padding: px2rem(15)
        box-sizing: border-box
        color: #999
        center()
      .popup-btn
        width: 100%
        height: px2rem(60)
        border-bottom: px2rem(1) solid #ccc
        font-size: px2rem(16)
        color: #666
        font-weight: bold
        center()
        &.danger
          color: $color-pink
</style>
